/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xlx.openLink.base.BasePage;
import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.conf.MysqlProperties;
import com.xlx.openLink.dao.BackupMapper;
import com.xlx.openLink.entity.pojo.Backup;
import com.xlx.openLink.entity.pojo.Manager;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.uitls.AssertUtils;
import com.xlx.openLink.vo.RequestManagerLst;
import com.xlx.openLink.vo.RequestSBackList;
import com.xlx.openLink.vo.RequestSystLst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class BackService {

    @Autowired
    private BackupMapper backupMapper;

    @Autowired
    private MysqlProperties mysqlProperties;

    public BasePage<Backup> listByPage(RequestSBackList request) {
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Backup> list = backupMapper.listByPage(request);
        PageInfo pageInfo = new PageInfo(list);
        return new BasePage<Backup>(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
    }

    /**
     * 删除
     *
     * @param pkGuid
     * @return
     */
    @Transactional
    public int delBackUp(String pkGuid) {
        Backup backup = backupMapper.selectByPrimaryKey(pkGuid);
        File f = new File(backup.getBakPath());
        f.delete();//删除文件
        return backupMapper.deleteByPrimaryKey(pkGuid);
    }

    public Backup selectById(String pkGuid) {
        Backup backup = backupMapper.selectByPrimaryKey(pkGuid);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.DATA_NOT_EXIST, "备份数据不存在"), backup);
        return backup;
    }

    /**
     * 上传数据
     *

     * @param name
     * @param id
     * @return
     */
    @Transactional
    public Backup upload(String name, String id, MultipartFile file) throws IOException {

        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), name);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), file);
        AssertUtils.isNotNull(new ThreadRunTimeExcption(ResultCodeEnum.ILLEGAL_PARAM_BLANK), id);
        if (!file.getName().endsWith(".sql")) {
            new ThreadRunTimeExcption(ResultCodeEnum.LBC_RESULT_BAD_REQUEST, "数据不符合格式");
        }
        String fileName  = BaseUtils.createGUID()+file.getOriginalFilename();
        String path = mysqlProperties.getBackPath();
        File t = new File(path + fileName);
        file.transferTo(t);
        Backup backup = new Backup();
        backup.setBakCreateDate(new Date());
        backup.setBakPath(mysqlProperties.getBackPath() + fileName);
        backup.setPkGuid(BaseUtils.createGUID());
        backup.setBakName(file.getOriginalFilename());
        backup.setBakUId(id);
        backup.setBakUName(name);
        backupMapper.insertSelective(backup);
        return backup;
    }


}
